﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using RuiXinCoffee.Model;

namespace RuiXinCoffee.DAL
{
    internal class ProductDAO : BaseDAO<Product>
    {
        //获取所有（用户）
        public List<Product> GetAllProducts()
        {
            string sql = "SELECT Id, Name, Price, Description, ImageUrl, CategoryId, IsOnSale FROM Product WHERE IsOnSale = 1";
            return SelectList(sql);
        }

        //根据分类
        public List<Product> GetProductsByCategory(int categoryId)
        {
            string sql = "SELECT Id, Name, Price, Description, ImageUrl, CategoryId, IsOnSale FROM Product WHERE CategoryId = @CategoryId AND IsOnSale = 1";
            MySqlParameter[] parameters = {
                new MySqlParameter("@CategoryId", categoryId)
            };
            return SelectList(sql, parameters);
        }

        //获取所有产品（管理员）
        public List<Product> GetAllProductsForAdmin()
        {
            string sql = "SELECT Id, Name, Price, Description, ImageUrl, CategoryId, IsOnSale FROM Product";
            return SelectList(sql);
        }

        //根据ID
        public Product GetProductById(int id)
        {
            string sql = "SELECT Id, Name, Price, Description, ImageUrl, CategoryId, IsOnSale FROM Product WHERE Id = @Id";
            MySqlParameter[] parameters = {
                new MySqlParameter("@Id", id)
            };
            return SelectSingle(sql, parameters);
        }

        //添加
        public int AddProduct(Product product)
        {
            string sql = "INSERT INTO Product (Name, Price, Description, ImageUrl, CategoryId, IsOnSale) VALUES (@Name, @Price, @Description, @ImageUrl, @CategoryId, @IsOnSale)";
            MySqlParameter[] parameters = {
                new MySqlParameter("@Name", product.Name),
                new MySqlParameter("@Price", product.Price),
                new MySqlParameter("@Description", product.Description),
                new MySqlParameter("@ImageUrl", product.ImageUrl),
                new MySqlParameter("@CategoryId", product.CategoryId),
                new MySqlParameter("@IsOnSale", product.IsOnSale)
            };
            return Insert(sql, parameters);
        }

        //更新
        public int UpdateProduct(Product product)
        {
            string sql = "UPDATE Product SET Name = @Name, Price = @Price, Description = @Description, ImageUrl = @ImageUrl, CategoryId = @CategoryId, IsOnSale = @IsOnSale WHERE Id = @Id";
            MySqlParameter[] parameters = {
                new MySqlParameter("@Id", product.Id),
                new MySqlParameter("@Name", product.Name),
                new MySqlParameter("@Price", product.Price),
                new MySqlParameter("@Description", product.Description),
                new MySqlParameter("@ImageUrl", product.ImageUrl),
                new MySqlParameter("@CategoryId", product.CategoryId),
                new MySqlParameter("@IsOnSale", product.IsOnSale)
            };
            return Update(sql, parameters);
        }

        //删除
        public int DeleteProduct(int id)
        {
            string sql = "DELETE FROM Product WHERE Id = @Id";
            MySqlParameter[] parameters = {
                new MySqlParameter("@Id", id)
            };
            return Delete(sql, parameters);
        }

        //获取所有分类
        public DataTable GetAllCategories()
        {
            string sql = "SELECT CategoryId, CategoryName FROM Category ORDER BY SortOrder";
            return Select(sql);
        }
    }
}
